﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MODEL;
using System.Data.SqlClient;

namespace DAL
{
    public class PinglunDAL
    {
        SqlHelper sqlhelper = new SqlHelper();

        public void Dr2Pinglun(DataRow row, PinglunModel model)
        {
            model.PID = (int)row["PID"];
            model.PFromSID = (int)row["PFromSID"];
            model.PToSID = (int)row["PToSID"];
            model.PIsDel = (bool)row["PIsDel"];
            model.PContent = (string)row["PContent"];
            model.PAddTime = (DateTime)row["PAddTime"];
            StudentModel stu = new StudentModel();
            new StudentDAL().Dr2Student(row, stu);
            model.PFromStudent = stu;
        }

        public int AddNewPinglun(string toSid, string txtContent, string fromSid)
        {
            string strSql = "insert into Pinglun(PFromSID,PToSID,PContent)  values(@fromSid,@toSid,@txtContent)";
            List<SqlParameter> paras = new List<SqlParameter>();
            paras.Add(new SqlParameter("@fromSid", fromSid));
            paras.Add(new SqlParameter("@toSid", toSid));
            paras.Add(new SqlParameter("@txtContent", txtContent));
            return sqlhelper.ExecuteNonQuery(strSql, paras.ToArray());
        }

        public List<PinglunModel> QueryByCondition(StudentModel model)
        {
            string strSql = "select * from pinglun left join Students on Pinglun.PFromSID=Students.SID where PIsDel=0 and PToSID=@PToSID";
            DataTable table = sqlhelper.ExecuteDataTable(strSql, new SqlParameter("@PToSID", model.SID));
            if(table.Rows.Count>0)
            {
                List<PinglunModel> list = new List<PinglunModel>();
                foreach (DataRow row in table.Rows)
                {
                    PinglunModel p = new PinglunModel();
                    Dr2Pinglun(row, p);
                    list.Add(p);
                }
                return list;
            }
            else
            {
                return null;
            }
        }
    }
}
